package com.qf.flash.base;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.base.WebViewFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 公用H5 Activity
 * 2018/5/4 15:31
 */
public class WebViewActivity extends BaseActivity
{
    @BindView(R.id.tv_top_title)
    TextView tv_top_title;
    @BindView(R.id.fl_web)
    FrameLayout fl_web;

    private WebViewFragment mFragment;

    private String mTitle;
    private String mUrl;

    public static void start(Activity activity, @NonNull String title, @NonNull String url)
    {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        activity.startActivity(intent);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initViewAndData() {
        initEventAndData();
    }

    @Override
    public int getFragmentGroupId()
    {
        return R.id.fl_web;
    }

    public void initEventAndData()
    {
        Intent intent = getIntent();
        if (intent == null)
            return;
        mTitle = intent.getStringExtra("title");
        mUrl = intent.getStringExtra("url");
        tv_top_title.setText(mTitle);
        if (!TextUtils.isEmpty(mUrl))
            mFragment = WebViewFragment.initWebViewFragment(this, mUrl, true);
    }

    @OnClick(R.id.tv_top_back)
    public void onViewClicked()
    {
        if (mFragment != null)
        {
            mFragment.onKeyDown(KeyEvent.KEYCODE_BACK);
        }
        else
        {
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (mFragment != null)
            return mFragment.onKeyDown(keyCode);
        return super.onKeyDown(keyCode, event);
    }
}
